<?php
if (!defined('IN_UkCMS')) exit('No direct script access allowed');

class Login extends Backend_Controller {

	function __construct() {
		parent::__construct();
	}

	function index() {
		if ($this->session->userdata('uid')) {
			redirect(setting('backend_access_point') . '/index');
		} else {
			$this->load->view('login');
		}
	}

	function quit() {
		$this->session->sess_destroy();
	}
	
	function do_post() {
		$this->load->model('uk/user_mdl');
		
		$username = $this->input->post('username', true);
		$password = $this->input->post('password', true);
		if ($username && $password) {
			$admin = $this->user_mdl->get_full_user_by_username($username);
			if ($admin) {
				if ($admin->password == md5($password)) {
					if ($admin->role == 1 && !setting('backend_root_access')) {
						$this->session->set_flashdata('error', "<b>系统限制了ROOT用户登录,请联系管理员!</b>");
						redirect(setting('backend_access_point') . '/login');
					} else {
						$this->session->set_userdata('uid', $admin->uid);
						redirect(setting('backend_access_point') . '/index');
					}
				} else {
					$this->session->set_flashdata('error', "<b>密码不正确!</b>");
					redirect(setting('backend_access_point') . '/login');
				}
			} else {
				$this->session->set_flashdata('error', '<b>不存在的用户!</b>');
				redirect(setting('backend_access_point') . '/login');
			}
		} else {
			$this->session->set_flashdata('error', '<b>用户名和密码不能为空!</b>');
			redirect(setting('backend_access_point') . '/login');
		}
	}

}